
#include <BProgram>
#include "member_BShader.h"
#include "member_BProgram.h"

using namespace BWE;

#define member					(*(member_BProgram*)_ptr)
#define member_allocate()		_ptr = new member_BProgram(this)
#define member_release()		delete (member_BProgram*)_ptr

BProgram::BProgram()
{
	member_allocate();
}
BProgram::~BProgram()
{
	member_release();
}

void BProgram::addShader(BShader* shader)
{
	if (!member.shaders.contain(shader))
	{
		member.shaders.append(shader);
		member.dirty = true;
	}
}
void BProgram::removeShader(const BShader* shader)
{
	if (member.shaders.remove(shader))
	{
		member.dirty = true;
	}
}
void BProgram::removeShader(const BString& name)
{
	for (int i = 0; i < member.shaders.size(); i++)
	{
		BShader* shader = member.shaders[i];
		if (shader->name() == name)
		{
			member.shaders.remove(shader);
			member.dirty = true;
			break;
		}
	}
}

int BProgram::shaderCount() const
{
	return member.shaders.size();
}
BShader* BProgram::shader(int index)
{
	return member.shaders(index);
}
const BShader* BProgram::shader(int index) const
{
	return member.shaders(index);
}

void BProgram::addUniform(BUniform* uniform)
{
	if (!member.uniforms.contain(uniform))
	{
		member.uniforms.append(uniform);
	}
}
void BProgram::removeUniform(BUniform* uniform)
{
	member.uniforms.remove(uniform);
}

int BProgram::uniformCount() const
{
	return member.uniforms.size();
}
BUniform* BProgram::uniform(int index)
{
	return member.uniforms(index);
}
const BUniform* BProgram::uniform(int index) const
{
	return member.uniforms(index);
}

void BProgram::clear()
{
	if (member.object)
		glDeleteProgram(member.object);
	member.object = -1;
	member.dirty = true;
}

